/**
 * 
 */
package co.gov.medellin.sdep.sisdep.cco.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.cco.exception.CentroComercialException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.VinculadoException;
import co.gov.medellin.sdep.sisdep.cco.exception.VinculadoRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.SolicitudLocalMessageDescriptor;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.VinculadoMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Vinculado;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IVinculadoDAOT;

/**
 * @author Usuario
 *
 */
public class VinculadoServiceImpl extends BaseService implements IVinculadoService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IVinculadoDAOT vinculadoDAO;

	/**
	 * 
	 */
	public VinculadoServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Vinculado vinculado) throws VinculadoException {
		try{
			vinculado.setUsuarioCrea(getPrincipalUser());
			vinculado.setFechaCrea(new Date());
			getVinculadoDAO().insert(vinculado);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new VinculadoException(new VinculadoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new VinculadoException(new VinculadoMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Vinculado vinculado) throws VinculadoException {
		try{
			vinculado.setUsuarioModifica(getPrincipalUser());
			vinculado.setFechaModifica(new Date());
			getVinculadoDAO().update(vinculado);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new VinculadoRuntimeException(new VinculadoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new VinculadoRuntimeException(new VinculadoMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Vinculado vinculado) throws VinculadoException {
		try {
			vinculado.setUsuarioModifica(getPrincipalUser());
			vinculado.setFechaModifica(new Date());
			getVinculadoDAO().delete(vinculado);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new VinculadoRuntimeException(new VinculadoMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Vinculado vinculado) throws VinculadoException {
		try{
			//return getVinculadoDAO().getAllVinculadoPaginated(start, max, criteria);
			return getVinculadoDAO().load(vinculado);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new VinculadoRuntimeException(new VinculadoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Vinculado get(long id) throws VinculadoException {
		try{
			return getVinculadoDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new VinculadoRuntimeException(new VinculadoMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List listaVinculados() throws VinculadoException {
		try{
			//return getSolicitudLocalDAO().getAllSolicitudLocalPaginated(start, max, criteria);
			return getVinculadoDAO().listaVinculados();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new SolicitudLocalRuntimeException(new SolicitudLocalMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}	
	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IVinculadoDAOT getVinculadoDAO() {
		return vinculadoDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setVinculadoDAO(IVinculadoDAOT vinculadoDAO) {
		this.vinculadoDAO = vinculadoDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
